#include"compile_run.hpp"
#include"../comm/log.hpp"
#include<httplib.h>

using namespace ns_compile_and_run;
using namespace ns_log;
using namespace httplib;

void Usage()
{
    LOG(ERROR) << "Usage: ./compile port";
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage();
        exit(-1);
    }
    Server ser;
    ser.Post("/compile_and_run", [](const Request &req, Response &resp){
        //post方法，我们需要的json串放在正文中
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json, out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });

    ser.listen("0.0.0.0", atoi(argv[1]));

    // Server svr;
    // svr.listen("0.0.0.0", 8080);

    // std::string in_json;
    // Json::Value in;
    // in["code"] = R"(#include<iostream>
    // int main()
    // {
    //     std::cout << "you can see me~~" << std::endl;
    //     return 0;
    // })";
    // in["input"] = "";
    // in["cpu_limit"] = 1;
    // in["mem_limit"] = 10240 * 5;

    // Json::FastWriter writer;
    // in_json = writer.write(in);
    // std::cout << in_json << std::endl;
    // std::string out;//存储返回的json串

    // CompileAndRun::Start(in_json, out);

    // std::cout << out << std::endl;

    // return 0;
}